package cn.com.darly.videoandroidpm.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.ClipboardManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.darly.common.DLog;
import com.darly.common.ToastApp;
import com.darly.dview.widget.camera.util.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.dao.ConversationMessageInfo;
import okhttp3.internal.Util;

/**
 * @author Darly/张宇辉/2018/3/14 16:11
 * @version 1.0/com.darly.darlyview.ui.adapter
 */

public class ConversationRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<ConversationMessageInfo> list;
    private OnRecyclerItemClickListener listener;
    private RecyclerView recyclerView;


    private LoginInfo friend;

    private LoginInfo user;

    public ConversationRecyclerAdapter(Context context, List<ConversationMessageInfo> list, LoginInfo friend, LoginInfo user) {
        this.context = context;
        this.list = list;
        this.friend = friend;
        this.user = user;
    }

    public void setOnRecyclerItemClickListener(OnRecyclerItemClickListener listener) {
        this.listener = listener;
    }

    public void setData(List<ConversationMessageInfo> data) {
        if (this.list!=null){
            this.list.addAll(data);
        }else {
            this.list = data;
        }
        notifyDataSetChanged();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ConversationMessageInfo.TYPE_BLE) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_conversation_left, parent, false);
            return new ChatLeftViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_conversation_right, parent, false);
            return new ChatRightViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        ConversationMessageInfo msg = list.get(position);
        String time = msg.getMsgTime().toString();
        final String content = msg.getMsgContent();
        if(holder instanceof ChatLeftViewHolder){
            ImageLoaderUtil.getInstance().loadImageNor(friend.getIcon(),((ChatLeftViewHolder) holder).id_converlift_img);
            ((ChatLeftViewHolder) holder).mTvMsgLeft.setText(content);
            ((ChatLeftViewHolder) holder).mTvMsgLeft.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View view) {
                    copyMessage(((ChatLeftViewHolder) holder).mTvMsgLeft,content);
                    return false;
                }
            });
        }else if(holder instanceof ChatRightViewHolder){
            ImageLoaderUtil.getInstance().loadImageNor(user.getIcon(),((ChatRightViewHolder) holder).id_converright_img);
            ((ChatRightViewHolder) holder).mTvMsgRight.setText(content);
            if (msg.isMsgSend()){
                ((ChatRightViewHolder) holder).id_converright_msg_send.setVisibility(View.GONE);
            }else {
                ((ChatRightViewHolder) holder).id_converright_msg_send.setVisibility(View.VISIBLE);
            }
            ((ChatRightViewHolder) holder).mTvMsgRight.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View view) {
                    copyMessage(((ChatRightViewHolder) holder).mTvMsgRight,content);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.parseInt(list.get(position).getMsgType());
    }



    @Override
    public int getItemCount() {
        return list ==null?0:list.size();
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null && listener != null) {
            int position = recyclerView.getChildAdapterPosition(v);
            listener.OnRecyclerItemClick(recyclerView, v, position, list.get(position));
        }
    }

    static class ChatLeftViewHolder extends RecyclerView.ViewHolder {
        ImageView id_converlift_img;
        TextView mTvMsgLeft;

        ChatLeftViewHolder(View view) {
            super(view);
            id_converlift_img = view.findViewById(R.id.id_converlift_img);
            mTvMsgLeft  = view.findViewById(R.id.id_converlift_msg);
        }
    }

    static class ChatRightViewHolder extends RecyclerView.ViewHolder{
        ImageView id_converright_img;
        ProgressBar id_converright_msg_send;
        TextView mTvMsgRight;

        ChatRightViewHolder(View view) {
            super(view);
            id_converright_img = view.findViewById(R.id.id_converright_img);
            id_converright_msg_send = view.findViewById(R.id.id_converright_msg_send);
            mTvMsgRight  = view.findViewById(R.id.id_converright_msg);
        }
    }

    /**
     * 自定义RecyclerView的点击事件
     */
    public interface OnRecyclerItemClickListener {
        void OnRecyclerItemClick(RecyclerView parent, View view, int position, ConversationMessageInfo data);
    }

    //添加消息显示在RecyclerView中
    public void addItem(ConversationMessageInfo msg) {
        if (list == null){
            this.list = new ArrayList<ConversationMessageInfo>();
        }
        list.add(msg);
        notifyDataSetChanged();
    }
    //消息发送成功
    public void msgSendOK(ConversationMessageInfo msg) {
        if (list ==null){
            this.list = new ArrayList<ConversationMessageInfo>();
            list.add(msg);
            notifyDataSetChanged();
        }else {
            for (ConversationMessageInfo infoer:list) {
                if (msg.getMsgKey().equals(infoer.getMsgKey())){
                    infoer.setMsgSend(true);
                    break;
                }
            }
            notifyItemChanged(getItemCount()-1);
        }

    }


    public void copyMessage(TextView view, final String msg){
        final PopupWindow pop = new PopupWindow(context);
        TextView tv = new TextView(context);
        tv.setText("复制");
        tv.setPadding(10,10,10,10);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setContentView(tv);
        pop.showAsDropDown(view);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy = (ClipboardManager) context
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText(msg);
                ToastApp.showToast(msg);
                pop.dismiss();
            }
        });
    }

}
